<?php
/*!
 * 
 * Form Class for creating HTML forms and rendering them into the HTML template
 * 
 */

if($script == '')
	$script = 'validate';
else if(is_array($script)) 
	array_push($script,'validate'); 
else {
	$script = array ($script,'validate');
} 

class Form {
	
	function __construct($name,$method='post',$stack=true,$attrs=array()) {
		$this->name = $name;
		$this->method = $method;
		$this->stack = $stack;
		$this->attrs = $attrs;
		$this->enctype = $this->attrs['enctype'];
	}
	
	private function add($data) {
		$this->body = $this->body.$data;
	}
	
	public function setClass($class_name) {
		$this->attrs['class'] = $class_name;
	}
	
	public function build() {
		if($this->stack == true) {
			$id = generateId($this->name);
			$div_attrs = $this->getAttrString($this->attrs);
			$start = "<div id='$id' $div_attrs><form name='$this->name' method='$this->method' enctype='$this->enctype'>";
			$end = "</form></div>";
		} else {
			$start = "<form name='$this->name' method='$this->method'>";
			$end = "</form>";
		}
		return $start.$this->body.$end;
	}
	
	private function mergeAttrs($attrs=array()) {
		$merged_attrs = $attrs;
		if(isset($attrs)) {
			foreach ($attrs as $key => $value) {
				if (strtolower($key) == 'class') {
					if($merged_attrs[$key] != $value)
					$value = $merged_attrs[$key] . ' ' . $value;
				} else if (strtolower($key) == 'style') {
					if($merged_attrs[$key] != $value)
					$value = $merged_attrs[$key] . ';' . $value;
				}
			}
			$merged_attrs[$key] = $value;
		}
		return $merged_attrs;
	}
	
	private function getAttrString($attrs=array()) {
		$attr_pairs = array();
		if(isset($attrs)) {
			foreach ($attrs as $name => $value) {
				$value = htmlentities($value, ENT_QUOTES);
				$attr_pairs[] = "$name=\"$value\"";
			}
		}
		return join(' ', $attr_pairs);
	}
	
	public function divStart($name,$attrs=array()) {
		$id = generateId($name);
		$attr_string = $this->getAttrString($attrs);
		$this->add("<div id='$id' $attr_string");
	}
	
	public function divEnd() {
		$this->add("</div>");
	}
	
	public function text($data) {
		$this->add($data);
	}
	
	public function textInput($name,$label,$attrs=array(),$stack=true) {
		$id = generateId($name);
		$type = $attrs['type']? $attrs['type'] : 'text';
		if(isset($attrs['description'])) { $description = $attrs['description']; unset($attrs['description']); }
		if(isset($attrs['class'])) { $div['class'] = $attrs['class']; unset($attrs['class']); }
		if(isset($attrs['style'])) { $div['style'] = $attrs['style']; unset($attrs['style']); }
		if($attrs['required'] == true) { $required = "<span class='required'>*</span>"; unset($attrs['required']); }
		$div_string = $this->getAttrString($this->mergeAttrs($div));
		$attr_string = $this->getAttrString($attrs);
		$labelStr = $label === ''? '' : "<label id='lbl-$id' for='$id'>$label: </label>";
		$body =	"$labelStr
			<input id='$id' name='$name' type='$type' $attr_string>
			$required
			<span id='validate-$id' class='invisible hover'></span>
			$description";
		if($stack===true) $this->add("<div id='row-$id' $div_string>$body</div>");
		else $this->add($body);
	}
	
	public function textArea($name,$label,$rows,$cols,$attrs=array(),$stack=true) {
		$id = generateId($name);
		if(isset($attrs['description'])) { $description = $attrs['description']; unset($attrs['description']); }
		if(isset($attrs['class'])) { $div['class'] = $attrs['class']; unset($attrs['class']); }
		if(isset($attrs['style'])) { $div['style'] = $attrs['style']; unset($attrs['style']); }
		if(isset($attrs['value'])) { $value = $attrs['value']; unset($attrs['value']); }
		$div_string = $this->getAttrString($this->mergeAttrs($div));
		$attr_string = $this->getAttrString($attrs);
		$labelStr = $label === ''? '' : "<label id='lbl-$id' class='textarea' for='$id'>$label: </label>";
		$body =	"$labelStr
			<textarea id='$id' name='$name' rows='$rows' cols='$cols' $attr_string>$value</textarea>
			<span id='validate-$id' class='invisible hover'></span>
			$description";
		if($stack===true) $this->add("<div id='row-$id' $div_string>$body</div>");
		else $this->add($body);
	}

	public function button($name,$label,$a=array(),$stack=true) {
		$id = generateId($name);
		if(isset($a['class'])) {
			if($stack == true) $div_class = "class = '{$a['class']}'";
			else $input_class = "class = '{$a['class']}'";
		}
		$onclick = $a['onclick']? "onclick='{$a['onclick']}'" : "";
		$type = $a['type']? $a['type'] : "submit";
		$body = "<input id='$id' name='$name' $input_class type='$type' $disabled value='$label' $onclick>";
		
		if($stack == true)
			$this->add("<div id='row-$id' $div_class>$body</div>");
		else $this->add($body);
	}
	
	public function fileUpload($name,$label,$attrs=array(),$stack=true) {
		$this->enctype = "multipart/form-data";
		$attrs['type'] = 'file';
		$this->textInput($name,$label,$attrs,$stack);
	}
}